package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.RouteDao;
import com.my12306.train.model.entity.RouteEntity;
import com.my12306.train.model.entity.StationEntity;
import com.my12306.train.service.RouteScheduleRelationService;
import com.my12306.train.service.RouteService;
import com.my12306.train.service.StationService;
import com.squareup.okhttp.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("routeService")
public class RouteServiceImpl extends ServiceImpl<RouteDao, RouteEntity> implements RouteService {

    @Autowired
    private RouteScheduleRelationService routeScheduleRelationService;
    @Autowired
    private MileageServiceImpl mileageService;
    @Autowired
    private StationService stationService;
    @Override
    public PageUtils queryRouteByPage(PageVo pageVo) {
        pageVo = pageVo.init();
        List<Route> routes = this.baseMapper.queryRouteByPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        if (routes != null) {
            Integer dataCount = queryRouteCounteByPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
            System.out.println("数据总量："+dataCount);
            return PageUtils.build(pageVo, routes, dataCount);
        }
        return null;
    }

    @Override
    public Integer queryRouteCounteByPage(PageVo pageVo, int logicDelete) {
        System.out.println(pageVo);
        System.out.println(logicDelete);
        Integer count = this.baseMapper.queryRouteCountByPage(pageVo, logicDelete);
        System.out.println(count);
        return count;
    }

    /**
     * 1.添加线路本身
     * @param route
     * @return
     */
    @Override
    public Long saveRoute(RouteEntity route) {
        if (route != null) {
            route.setRouteId(SnowflakeIdWorker.generateId());
            route.setCreateDate(new Date());
            route.setUpdateDate(new Date());
           StationEntity stationEntity =  stationService.queryStationById(route.getStationNo());
            if (stationEntity!=null){
                route.setStationName(stationEntity.getStationName());
            }else{
                route.setStationName("错误结果！");
            }
            int insert = this.baseMapper.insert(route);
            System.out.println("route 添加后的结果："+insert);
        }
        return null;
    }

    /**
     * 修改线路基本信息
     *
     * @param route
     */
    @Override
    public void  updateRouteById(RouteEntity route) {
        if (route != null) {
            route.setUpdateDate(new Date());
            this.baseMapper.updateById(route);
        }
    }

    /**
     * 批量删除线路
     * 1.逻辑删除线路本身
     * 2.删除线路与车次的管理
     * 3.删除线路与里程的管理
     *
     * @param roleIds
     * @return
     */
    @Override
    @Transactional
    public ResponseData deleteRouteBatchByIds(List<Long> roleIds) {
        //1.逻辑删除线路本身
        this.baseMapper.deleteRouteBatchByIds(roleIds, CommonConstants.LogicDelete.no_delet.getValue());
        //2.删除线路于车次管理。
        routeScheduleRelationService.deleteRouteScheduleByRouteId(roleIds,CommonConstants.LogicDelete.no_delet.getValue());
        //3.删除线路与里程的管理
        mileageService.deleteMileageByRouteId(roleIds);
        return ResponseData.ok();
    }

    @Override
    public List<RouteEntity> queryAllRoute(int logicDelete) {
        return this.baseMapper.queryRouteAll(logicDelete);
    }

    @Override
    public List<StationEntity> getAddParamters() {
     return stationService.queryAllStation(CommonConstants.LogicDelete.no_delet.getValue());
    }

    @Override
    public RouteEntity queryRouteByRouteId(Long routeId) {
        QueryWrapper<RouteEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
        queryWrapper.eq("route_id",routeId);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
